package com.huilan.hgt.service.impl;

import com.huilan.hgt.entity.Attachment;
import com.huilan.hgt.entity.AttachmentCatalogue;
import com.huilan.hgt.mapper.AttachmentCatalogueMapper;
import com.huilan.hgt.mapper.AttachmentMapper;
import com.huilan.hgt.service.AttachmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huilan.hgt.util.Configurations;
import com.huilan.hgt.util.PageUtil;
import com.huilan.hgt.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 附件表 服务实现类
 * </p>
 *
 * @author ghy
 * @since 2018-11-02
 */
@Service
public class AttachmentServiceImpl extends ServiceImpl<AttachmentMapper, Attachment> implements AttachmentService {

    @Autowired
    AttachmentMapper attachmentMapper;

    @Autowired
    AttachmentCatalogueMapper attachmentCatalogueMapper;

    /**
     * 根据ID获取附件文件
     *
     *
     * @param id
     * @return
     */
    @Override
    public File getFile(String id) {
        Attachment attachment = this.getById(id);
        return new File(attachment.getAttachmentPath());
    }

    /**
     * 根据附件code获取附件信息
     *
     * @param code
     * @return
     */
    @Override
    public List<Attachment> getAttachmentByCode(String code) {
        Map<String,Object> params = new HashMap<>();
        params.put(Attachment.ATTACHMENT_CODE,code);

        List<Attachment> attachments = attachmentMapper.selectByMap(params);
        return attachments;
    }

    /**
     * 根据邮件ID查询附件信息
     *
     * @param emailId
     * @return
     */
    @Override
    public List<Attachment> queryAttachmentByEmailId(String emailId) {
        return attachmentMapper.queryAttachmentByEmailId(emailId);
    }

    /**
     * 查询所有附件
     *
     * @param page
     * @param catalogueId
     * @return
     */
    @Override
    public List<Attachment> queryAttachmentList(PageUtil page, Integer catalogueId) {

        return attachmentMapper.queryAttachmentList(page,catalogueId);
    }

    /**
     * 保存附件信息
     *
     * @param fileName
     * @param code
     * @param start
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savaAttachmentInfo(String fileName, String code, long start) {
        //保存附件信息
        Attachment attachment = new Attachment();
        attachment.setAttachmentPath(Configurations.getFileRepository()+ File.separator+fileName)
                .setAttachmentCode(code)
                .setAttachmentName(fileName)
                .setAttachmentSize(StringUtil.convertFileSize(start));
        attachmentMapper.insert(attachment);

        //保存附件目录中见表信息
        AttachmentCatalogue attachmentCatalogue = new AttachmentCatalogue();
        attachmentCatalogue.setAttachmentId(attachment.getAttachmentId())
                .setCatalogueId(AttachmentCatalogue.PERSONAL);
        attachmentCatalogueMapper.insert(attachmentCatalogue);
    }
}
